package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.pojo.Test;
import com.tanhua.server.service.SoulTestService;
import com.tanhua.common.pojo.TestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/testSoul")
@Slf4j
public class SoulController {
    @Autowired
    private SoulTestService soulTestService;

    @GetMapping
    public ResponseEntity<List<Test>> queryTest() {
        try {
            List<Test> testList = soulTestService.queryTest();
            if (ObjectUtil.isNotEmpty(testList)) {
                return ResponseEntity.ok(testList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping
    public ResponseEntity<String> submitTest(@RequestBody String json) {
        try {
            String s = soulTestService.submitTest(json);
            if (ObjectUtil.isNotEmpty(s)) {
                return ResponseEntity.ok(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @GetMapping("/report/{id}")
    public ResponseEntity<TestResult> queryResult(@PathVariable("id") String id) {
        try {
            TestResult testResult = soulTestService.queryResult(id);
            if (ObjectUtil.isNotEmpty(testResult)) {
                return ResponseEntity.ok(testResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
